package com.kobeliu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.enums.PaymentStatus;
import com.kobeliu.enums.YesOrNo;
import com.kobeliu.mapper.MerchantOrdersMapper;
import com.kobeliu.pojo.MerchantOrders;
import com.kobeliu.pojo.bo.MerchantOrderBO;
import com.kobeliu.service.PaymentOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class PaymentOrderServiceImpl implements PaymentOrderService {

    @Autowired
    private MerchantOrdersMapper merchantOrdersMapper;

    @Transactional
    @Override
    public boolean createPaymentOrder(MerchantOrderBO bo) {
        MerchantOrders pending = new MerchantOrders();
        BeanUtils.copyProperties(bo,pending);

        pending.setPayMethod(PaymentStatus.WAIT_PAY.type);
        pending.setIsDelete(YesOrNo.NO.type);
        pending.setCreateTime(LocalDateTime.now());
        pending.setUpdatedTime(LocalDateTime.now());

        int result = merchantOrdersMapper.insert(pending);

        return result==1;
    }

    @Override
    public MerchantOrders queryOrderInfo(String merchantOrderId, PaymentStatus waitPay) {

        QueryWrapper<MerchantOrders> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("merchant_order_id",merchantOrderId);

        if(waitPay != null){
            queryWrapper.eq("pay_status",waitPay.type);
        }

        return merchantOrdersMapper.selectOne(queryWrapper);



    }
}
